package com.example.testlib

// 作用域函数
object ScopeFunctionExample {
    data class Car(
        var brand: String,
        var price: Int
    )

    var car: Car? = null
    fun funcExample() {
        car = Car("红旗", 199999)
        val carBrand = car?.let { "car's brand is ${it.brand}" }
        println(carBrand)
        val carPrice = car?.run { "car's price is ${this.price}" }
        println(carPrice)

        car?.also {
            println("car 贴牌前 brand = ${it.brand}")
        }?.brand = "比亚迪"
        car?.apply {
            println("car 贴牌后 brand = ${this.brand}")
        }?.apply {
            println("car's price = ${this.price}")
        }

        car?.takeIf { it.price > 1500000 }
            ?.also { println("车太贵啦！") }
            ?: run { println("价格还行！") }

        repeat(3) {
            println("car' brand is ${car?.brand}, price is ${car?.price} 当前执行索引为：$it")
        }


    }

}



